﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmCustomerUpdate : Form
    {
        public int uCustomer = -1;
        public string modifyComment = "";
        public DateTime contractEndDate = DateTime.MinValue;
        public FrmCustomerUpdate()
        {
            InitializeComponent();
            uCustomer = -1;
            this.Text = "新建一条客户信息";
            button_terminatecontact.Enabled = false;
            //insert
        }
        public FrmCustomerUpdate(int id)
        {
            InitializeComponent();
            uCustomer = id;
            this.Text = "更新客户"+id+"的信息";
            button_terminatecontact.Enabled = true;
            //update
        }
        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button_confirm_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (uCustomer > 0)
            {

                //update
                sql = "UPDATE [CJ_CUSTOMER_INFO] "+
                    "   SET [ROOM_NO] = @ROOM_NO "+
                    "      ,[COMPANY_NAME] = @COMPANY_NAME "+
                    "      ,[CONTRACT_NO] = @CONTRACT_NO "+
                    "      ,[CONTRACT_START_DATE] = @CONTRACT_START_DATE "+
                    "      ,[CONTRACT_END_DATE] = @CONTRACT_END_DATE "+
                    "      ,[RENT_AREA] = @RENT_AREA "+
                    "      ,[MONTHLY_RENTAL] = @MONTHLY_RENTAL "+
                    "      ,[WUYE] = @WUYE "+
                    "      ,[YEARLY_RENTAL] = @YEARLY_RENTAL "+
                    "      ,[PARKING_COUNT] = @PARKING_COUNT "+
                    "      ,[PARKING_DETAIL] = @PARKING_DETAIL "+
                    "      ,[CORP_REP] = @CORP_REP "+
                    "      ,[CONTACT] = @CONTACT "+
                    "      ,[OFFICE_TELEORFAX] = @OFFICE_TELEORFAX "+
                    "      ,[MOBILE_PHONE] = @MOBILE_PHONE "+
                    "      ,[REGISTERED_CAPITAL] = @REGISTERED_CAPITAL "+
                    "      ,[REGISTERED_PLACE] = @REGISTERED_PLACE "+
                    "      ,[TAX_ID] = @TAX_ID "+
                    "      ,[IC_ID] = @IC_ID "+
                    "      ,[ORG_CODE] = @ORG_CODE "+
                    "      ,[INDUSTRY_CATEGORY] = @INDUSTRY_CATEGORY "+
                    "      ,[MAIN_BUZZ] = @MAIN_BUZZ "+
                    "      ,[LAST_MODIFY_DATE] = @LAST_MODIFY_DATE "+
                    "      ,[MODIFY_COMENT] = @MODIFY_COMENT "+
                    " WHERE ID=@ID";
                using (DataBase db = new DataBase())
                {
                    db.AddParameter("ID", uCustomer);

                    db.AddParameter("ROOM_NO", textBox_room.Text);
                    db.AddParameter("COMPANY_NAME", textBox_name.Text);
                    db.AddParameter("CONTRACT_NO", textBox_contract.Text);
                    db.AddParameter("CONTRACT_START_DATE", dateTimePicker_start.Value);
                    db.AddParameter("CONTRACT_END_DATE", dateTimePicker_end.Value);
                    db.AddParameter("RENT_AREA", textBox_area.Text);
                    db.AddParameter("MONTHLY_RENTAL", textBox_month.Text);
                    db.AddParameter("WUYE", textBox_wuye.Text);
                    db.AddParameter("YEARLY_RENTAL", textBox_year.Text);
                    db.AddParameter("PARKING_COUNT", textBox_parkcnt.Text);
                    db.AddParameter("PARKING_DETAIL", textBox_parkdetail.Text);
                    db.AddParameter("CORP_REP", textBox_corprep.Text);
                    db.AddParameter("CONTACT", textBox_contact.Text);
                    db.AddParameter("OFFICE_TELEORFAX", textBox_telfax.Text);
                    db.AddParameter("MOBILE_PHONE", textBox_mobile.Text);
                    db.AddParameter("REGISTERED_CAPITAL", textBox_capital.Text);
                    db.AddParameter("REGISTERED_PLACE", textBox_place.Text);
                    db.AddParameter("TAX_ID", textBox_taxid.Text);
                    db.AddParameter("IC_ID", textBox_icid.Text);
                    db.AddParameter("ORG_CODE", textBox_orgcode.Text);
                    db.AddParameter("INDUSTRY_CATEGORY", comboBox_category.Text);
                    db.AddParameter("MAIN_BUZZ", textBox_mainbuzz.Text);
                    db.AddParameter("LAST_MODIFY_DATE", DateTime.Now);
                    db.AddParameter("MODIFY_COMENT", modifyComment+";"+DateTime.Now.ToString()+" "+userinfo.username+" 更改了该记录");
                    if (db.ExecuteNonQuery(sql) == 1)
                    {
                        MessageBox.Show("更新成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("更新失败");
                    }
                }
            }
            else
            {
                //insert
                sql = "INSERT INTO [CJ_CUSTOMER_INFO]([ROOM_NO],[COMPANY_NAME],[CONTRACT_NO],[CONTRACT_START_DATE] " +
                    ",[CONTRACT_END_DATE],[RENT_AREA],[MONTHLY_RENTAL],[WUYE],[YEARLY_RENTAL] " +
                    ",[PARKING_COUNT],[PARKING_DETAIL],[CORP_REP],[CONTACT],[OFFICE_TELEORFAX] " +
                    ",[MOBILE_PHONE],[REGISTERED_CAPITAL],[REGISTERED_PLACE],[TAX_ID],[IC_ID] " +
                    ",[ORG_CODE],[INDUSTRY_CATEGORY],[MAIN_BUZZ],[OP_NAME],[OP_ID],[LOG_TIME] " +
                    ",[LAST_MODIFY_DATE],[MODIFY_COMENT],[CONTRACT_TERMINATE]) VALUES " +
                    "(@ROOM_NO,@COMPANY_NAME,@CONTRACT_NO,@CONTRACT_START_DATE,@CONTRACT_END_DATE,@RENT_AREA " +
                    ",@MONTHLY_RENTAL,@WUYE,@YEARLY_RENTAL,@PARKING_COUNT,@PARKING_DETAIL,@CORP_REP " +
                    ",@CONTACT,@OFFICE_TELEORFAX,@MOBILE_PHONE,@REGISTERED_CAPITAL,@REGISTERED_PLACE " +
                    ",@TAX_ID,@IC_ID,@ORG_CODE,@INDUSTRY_CATEGORY,@MAIN_BUZZ,@OP_NAME " +
                    ",@OP_ID,@LOG_TIME,@LAST_MODIFY_DATE,@MODIFY_COMENT,@CONTRACT_TERMINATE)";
                using (DataBase db = new DataBase())
                {
                    db.AddParameter("ROOM_NO", textBox_room.Text);
                    db.AddParameter("COMPANY_NAME", textBox_name.Text);
                    db.AddParameter("CONTRACT_NO", textBox_contract.Text);
                    db.AddParameter("CONTRACT_START_DATE", dateTimePicker_start.Value);
                    db.AddParameter("CONTRACT_END_DATE", dateTimePicker_end.Value);
                    db.AddParameter("RENT_AREA", textBox_area.Text);
                    db.AddParameter("MONTHLY_RENTAL", textBox_month.Text);
                    db.AddParameter("WUYE", textBox_wuye.Text);
                    db.AddParameter("YEARLY_RENTAL", textBox_year.Text);
                    db.AddParameter("PARKING_COUNT", textBox_parkcnt.Text);
                    db.AddParameter("PARKING_DETAIL", textBox_parkdetail.Text);
                    db.AddParameter("CORP_REP", textBox_corprep.Text);
                    db.AddParameter("CONTACT", textBox_contact.Text);
                    db.AddParameter("OFFICE_TELEORFAX", textBox_telfax.Text);
                    db.AddParameter("MOBILE_PHONE", textBox_mobile.Text);
                    db.AddParameter("REGISTERED_CAPITAL", textBox_capital.Text);
                    db.AddParameter("REGISTERED_PLACE", textBox_place.Text);
                    db.AddParameter("TAX_ID", textBox_taxid.Text);
                    db.AddParameter("IC_ID", textBox_icid.Text);
                    db.AddParameter("ORG_CODE", textBox_orgcode.Text);
                    db.AddParameter("INDUSTRY_CATEGORY", comboBox_category.Text);
                    db.AddParameter("MAIN_BUZZ", textBox_mainbuzz.Text);
                    db.AddParameter("LAST_MODIFY_DATE", DateTime.Now);
                    db.AddParameter("MODIFY_COMENT", DateTime.Now.ToString() + " " + userinfo.username + " 创建了该记录");
                    db.AddParameter("OP_NAME", userinfo.username);
                    db.AddParameter("OP_ID", userinfo.id);
                    db.AddParameter("LOG_TIME", DateTime.Now);
                    db.AddParameter("CONTRACT_TERMINATE", false);
                    if (db.ExecuteNonQuery(sql) == 1)
                    {
                        MessageBox.Show("创建记录成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("创建记录失败");
                    }

                }
            }
        }

        private void FrmCustomerUpdate_Load(object sender, EventArgs e)
        {
            string sql = "";
            if (uCustomer > 0)
            {
                //first get info from present record in db
                sql = "SELECT [ROOM_NO],[COMPANY_NAME],[CONTRACT_NO],[CONTRACT_START_DATE],[CONTRACT_END_DATE] " +
                    ",[RENT_AREA],[MONTHLY_RENTAL],[WUYE],[YEARLY_RENTAL],[PARKING_COUNT],[PARKING_DETAIL] " +
                    ",[CORP_REP],[CONTACT],[OFFICE_TELEORFAX],[MOBILE_PHONE],[REGISTERED_CAPITAL],[REGISTERED_PLACE] " +
                    ",[TAX_ID],[IC_ID],[ORG_CODE],[INDUSTRY_CATEGORY],[MAIN_BUZZ],[MODIFY_COMENT],[CONTRACT_TERMINATE] " +
                    "FROM [CJ_CUSTOMER_INFO] WHERE ID=" + uCustomer;
                
                using (DataBase db = new DataBase())
                {
                    DataTable dt = db.ExecuteDataTable(sql);
                    if (dt.Rows.Count != 1)
                    {
                        MessageBox.Show("无此条记录!");
                        this.Close();
                        return;
                    }
                    
                    if (dt.Rows[0][23].ToString() == "True")
                    {
                        MessageBox.Show("合同已终止");
                    }
                    
                    DateTime startdate = DateTime.MaxValue, enddate = DateTime.MinValue;
                    DateTime.TryParse(dt.Rows[0][3].ToString(), out startdate);
                    DateTime.TryParse(dt.Rows[0][4].ToString(), out enddate);
                    contractEndDate = enddate;
                    if (startdate > DateTime.Now)
                        MessageBox.Show("该合同尚未生效");
                    if (enddate < DateTime.Now)
                        MessageBox.Show("该合同终止日期已到，请尽快与商户联系");

                    modifyComment = dt.Rows[0][22].ToString();

                    textBox_room.Text = dt.Rows[0][0].ToString();
                    textBox_name.Text = dt.Rows[0][1].ToString();
                    textBox_contract.Text = dt.Rows[0][2].ToString();
                    dateTimePicker_start.Value = startdate;
                    dateTimePicker_end.Value = enddate;
                    textBox_area.Text = dt.Rows[0][5].ToString();
                    textBox_month.Text = dt.Rows[0][6].ToString();
                    textBox_wuye.Text = dt.Rows[0][7].ToString();
                    textBox_year.Text = dt.Rows[0][8].ToString();
                    textBox_parkcnt.Text = dt.Rows[0][9].ToString();
                    textBox_parkdetail.Text = dt.Rows[0][10].ToString();
                    textBox_corprep.Text = dt.Rows[0][11].ToString();
                    textBox_contact.Text = dt.Rows[0][12].ToString();
                    textBox_telfax.Text = dt.Rows[0][13].ToString();
                    textBox_mobile.Text = dt.Rows[0][14].ToString();
                    textBox_capital.Text = dt.Rows[0][15].ToString();
                    textBox_place.Text = dt.Rows[0][16].ToString();
                    textBox_taxid.Text = dt.Rows[0][17].ToString();
                    textBox_icid.Text = dt.Rows[0][18].ToString();
                    textBox_orgcode.Text = dt.Rows[0][19].ToString();
                    comboBox_category.Text = dt.Rows[0][20].ToString();
                    textBox_mainbuzz.Text = dt.Rows[0][21].ToString();
                }
            }
        }

        private void button_terminatecontact_Click(object sender, EventArgs e)
        {
            if (uCustomer < 0)
                return;
            if (contractEndDate > DateTime.Now)
            {
                DialogResult dr = MessageBox.Show("合同终止日期尚未到达，确定要终止该合同吗?", "合同终止确认", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.Cancel)
                    return;
            }
            else
            {
                DialogResult dr = MessageBox.Show("确定要终止该合同吗?", "合同终止确认", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.Cancel)
                    return;
            }
            string sql = "UPDATE [CJ_CUSTOMER_INFO] " +
                    "   SET [CONTRACT_TERMINATE]=@CONTRACT_TERMINATE "+
                    " WHERE ID=@ID";
            using (DataBase db = new DataBase())
            {
                db.AddParameter("CONTRACT_TERMINATE", true);
                db.AddParameter("ID", uCustomer);
                if (db.ExecuteNonQuery(sql) == 1)
                {
                    MessageBox.Show("合同成功终止!");
                    this.Close();
                }
            }
        }
    }
}
